<html>

<style>
    * {
        margin: 0;
    }

    #content {
        color: white;
        position: absolute;
        left: -20px;
        top: -10px;
        white-space: pre-wrap;
        height: 200px;
        overflow: auto;
    }
</style>

<script type="importmap">
	{
	  "imports": {
		"three": "../../three.js-r151/build/three.module.js",
		"three/addons/": "../../three.js-r151/examples/jsm/"
	  }
	}
</script>

<body>
    <div id="content">
        canvas是不是没法用html.onClick？
        而且3d模型，空间的，是否没法穿透？
        intersectObjects 用的很常见
    </div>
    <div id="canvas"></div>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
    

        // 1. 必须创建的三大件
        // 1.1 三维场景Scene, 类似创建了个世界，三维世界
        const scene = new THREE.Scene();
        const geometry = new THREE.BoxGeometry(1, 1, 1);
        const material = new THREE.MeshBasicMaterial({ color: 'yellow' });
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        const material1 = new THREE.MeshBasicMaterial({ color: 'yellow' });
        const cube1 = new THREE.Mesh(geometry, material1);
        cube1.position.set(0, 0, 2)
        scene.add(cube1);

        // 添加辅助坐标系
        const axesHelper = new THREE.AxesHelper(5);
        scene.add(axesHelper);

        // 1.2 相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(3, 3, 3)

        // 1.3 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        let canvasDom = document.getElementById("canvas");
        canvasDom.appendChild(renderer.domElement);

        // 2. 设置相机控件轨道控制器OrbitControls
        new OrbitControls(camera, canvasDom);

        // 3. 射线相交拾取
        // 创建射线对象Ray
        const raycaster = new THREE.Raycaster()
        // 设置射线起点
        raycaster.ray.origin = new THREE.Vector3(-5, 0, 0);
        // 设置射线方向射线方向沿着x轴
        raycaster.ray.direction = new THREE.Vector3(1, 0, 0);

        console.log("raycaster", raycaster);
        const intersects = raycaster.intersectObjects([cube, cube1]);
        if (intersects.length > 0) {
            // 选中模型的第一个模型，设置为红色
            intersects[0].object.material.color.set(0xff0000);
        }

        function render() {
            renderer.render(scene, camera);
            requestAnimationFrame(render)
        }
        render();
    </script>
</body>

</html>